package com.zzt.lotterysystem.service.activitystatus.operator;

import com.zzt.lotterysystem.dao.dataobject.ActivityDO;
import com.zzt.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.zzt.lotterysystem.dao.mapper.ActivityMapper;
import com.zzt.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.zzt.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.zzt.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.zzt.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-05-29
 * Time: 16:23
 */
@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        Long activityId  = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();

        if (null == activityId || null == targetActivityStatus) {
            return false;
        }

        ActivityDO activityDO = activityMapper.selectById(activityId);

        if (null == activityDO) {
            return false;
        }

        // 需要判断奖品是否全部抽完
        // 查询 INIT 状态的奖品个数
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if (count > 0) {
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        try {
            activityMapper.updateStatus(activityId, targetActivityStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
